<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="700px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="150px">
          <el-form-item label="名称" prop="sponsorName">
            <el-input v-model="form.sponsorName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="编码" prop="sponsorCode">
            <el-input v-model="form.sponsorCode" style="width: 370px;" />
          </el-form-item>

          <el-form-item label="省市区" prop="areaId">
            <el-input
              v-if="false"
              v-model="form.areaId"
              style="width: 370px;"
            />
            <div v-if="form.areaId">
              {{ dialogTableDataAreaId.showInputValue }}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDataAreaId"
              @select-callback="dialogTableSelectedCallBackAreaId"
            ></dialogTable>
          </el-form-item>
          <el-form-item label="地址" prop="sponsorAddress">
            <el-input v-model="form.sponsorAddress" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="联系方式" prop="sponsorPhone">
            <el-input v-model="form.sponsorPhone" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="商家类型" prop="sponsorType">
            <el-select v-model="form.sponsorType" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.SPONSOR_TYPE"
                :key="item.id"
                :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="商家主页业务" prop="sponsorMainBusiness">
            <el-input v-model="form.sponsorMainBusiness" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="商家/赞助详情">
            <el-input v-model="form.sponsorDetail" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="合作方案">
            <el-input v-model="form.cooperationDetails" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="合作开始时间">
            <el-date-picker v-model="form.cooperationStartDate" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="合作结束日期">
            <el-date-picker v-model="form.cooperationEndDate" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="签约人" prop="signPrincipalMember">
            <el-input v-model="form.signPrincipalMember" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="签约人电话" prop="signPrincipalMemberPhone">
            <el-input v-model="form.signPrincipalMemberPhone" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="附件">
            <el-input v-model="form.accessory" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="备注说明">
            <el-input v-model="form.remark" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('sponsorName')" prop="sponsorName" label="名称" />
        <el-table-column v-if="columns.visible('sponsorCode')" prop="sponsorCode" label="编码" />
        <el-table-column v-if="columns.visible('sponsorType')" prop="sponsorType" label="商家类型">
          <template slot-scope="scope">
            {{ dict.label.SPONSOR_TYPE[scope.row.sponsorType] }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('sponsorPhone')" prop="sponsorPhone" label="联系方式" />
        <el-table-column v-if="columns.visible('sponsorMainBusiness')" prop="sponsorMainBusiness" label="商家主页业务" />

        <el-table-column v-if="columns.visible('areaName')" prop="areaName" label="省市区" />
        <el-table-column v-if="columns.visible('sponsorAddress')" prop="sponsorAddress" label="地址" />

        <el-table-column v-if="columns.visible('sponsorDetail')" prop="sponsorDetail" label="商家/赞助详情" />
        <el-table-column v-if="columns.visible('cooperationDetails')" prop="cooperationDetails" label="合作方案" />
        <el-table-column v-if="columns.visible('cooperationStartDate')" prop="cooperationStartDate" label="合作开始时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.cooperationStartDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('cooperationEndDate')" prop="cooperationEndDate" label="合作结束日期">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.cooperationEndDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('signPrincipalMember')" prop="signPrincipalMember" label="签约人" />
        <el-table-column v-if="columns.visible('signPrincipalMemberPhone')" prop="signPrincipalMemberPhone" label="签约人电话" />
        <el-table-column v-if="columns.visible('accessory')" prop="accessory" label="附件" />
        <el-table-column v-if="columns.visible('remark')" prop="remark" label="备注说明" />

        <el-table-column v-permission="['admin','tanzerSponsor:edit','tanzerSponsor:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudTanzerSponsor from '@/api/tanzer/tanzerSponsor'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";
import dialogTable from "@/views/components/dialogTable/index.vue";

// crud交由presenter持有
const defaultCrud = CRUD({ title: '合作商家', url: 'api/tanzerSponsor', sort: 'id,desc', crudMethod: { ...crudTanzerSponsor }})
const defaultForm = { id: null, sponsorName: null, sponsorCode: null, areaId: null, sponsorAddress: null, sponsorPhone: null, sponsorType: null, sponsorMainBusiness: null, sponsorDetail: null, cooperationDetails: null, cooperationStartDate: null, cooperationEndDate: null, signPrincipalMember: null, accessory: null, remark: null, isDel: null, createTime: null, createBy: null, updateBy: null, updateTime: null }
export default {
  name: 'TanzerSponsor',
  components: {dialogTable, pagination, crudOperation, rrOperation, udOperation ,MaterialList},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  dicts: ['SPONSOR_TYPE'],
  data() {
    return {

      permission: {
        add: ['admin', 'tanzerSponsor:add'],
        edit: ['admin', 'tanzerSponsor:edit'],
        del: ['admin', 'tanzerSponsor:del']
      },
      rules: {
        sponsorName: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        sponsorCode: [
          { required: true, message: '编码不能为空', trigger: 'blur' }
        ],
        areaId: [
          { required: true, message: '省市区不能为空', trigger: 'blur' }
        ],
        sponsorAddress: [
          { required: true, message: '地址不能为空', trigger: 'blur' }
        ],
        sponsorPhone: [
          { required: true, message: '联系方式不能为空', trigger: 'blur' }
        ],
        sponsorType: [
          { required: true, message: '商家类型不能为空', trigger: 'blur' }
        ],
        sponsorMainBusiness: [
          { required: true, message: '商家主页业务不能为空', trigger: 'blur' }
        ],
        signPrincipalMember: [
          { required: true, message: '签约人不能为空', trigger: 'blur' }
        ],
        signPrincipalMemberPhone: [
          { required: true, message: '签约人电话不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'sponsorName', display_name: '名称' },
        { key: 'sponsorCode', display_name: '编码' },
        { key: 'areaId', display_name: '省市区' },
        { key: 'sponsorAddress', display_name: '地址' },
        { key: 'sponsorPhone', display_name: '联系方式' },
        { key: 'sponsorType', display_name: '商家类型' },
        { key: 'signPrincipalMember', display_name: '签约人' },
        { key: 'signPrincipalMemberPhone', display_name: '签约人电话' }
      ],
      //自定义lov数据源
      dialogTableDataAreaId: {
        title: "省市区",
        dialogVisible: false,
        url: "api/tanzerBaseRegion",
        sort: "id,desc",
        buttonContext: "请选择省市区",
        showInputValue: "",
        queryTypeOptions: [
          { key: 'provinceName', display_name: '省级名称' },
          { key: 'cityName', display_name: '市级名称' },
          { key: 'districtName', display_name: '区级名称' },
        ],
        elTtableColumns: [
          {
            columnProp: "provinceName",
            columnLabel: "省级名称"
          },
          {
            columnProp: "cityName",
            columnLabel: "市级名称"
          },
          {
            columnProp: "districtName",
            columnLabel: "区级名称"
          }
        ]
      },
    }
  },
  watch: {
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }else{
        delete this.crud.params.sponsorName
        delete this.crud.params.sponsorCode
        delete this.crud.params.areaId
        delete this.crud.params.sponsorAddress
        delete this.crud.params.sponsorPhone
        delete this.crud.params.sponsorType
        delete this.crud.params.signPrincipalMember
      }
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
    },

    [CRUD.HOOK.afterToEdit](crud, form) {

      this.setInputValue(
        this.dialogTableDataAreaId,
        "showInputValue",
        form,
        "areaId",
        "content",
        0,
        "areaName",
        "api/tanzerBaseRegion",
        {
          page: 0,
          size: 10,
          sort: "id,desc",
          id: form.areaId
        }
      );
    },

    //省市区数据源
    dialogTableSelectedCallBackAreaId(row) {
      this.form.areaId = row.id;
      if (this.form.areaId) {
        this.dialogTableDataAreaId.showInputValue = row.areaName;
      }
    },
  }
}



</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
